﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Jidong
{
    public partial class 备件品种录入 : System.Web.UI.Page
    {
        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 0;
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }
                if (id > 0)
                {
                    this.SqlDataSource2.SelectParameters["ID"].DefaultValue = id.ToString();
                    this.SqlDataSource2.Select(DataSourceSelectArguments.Empty);
                }
            }
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void PinMing_TextChanged(object sender, EventArgs e)
        {
            this.PinMing.Text=this.PinMing.Text.Trim();
            if (this.PinMing.Text != "")
            {
                this.SqlDataSource1.SelectCommand = "";
                this.SqlDataSource1.SelectCommand = "select * from 物料品种 where 品名 like '%" + PinMing.Text.Replace("%", "[%]").Replace("'", "''") + "%'";
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            if (this.PinMing.Text == "")
            {
                this.Notice = "“品名”必须录入";
                return;
            }
            int qz;
            if (!int.TryParse(this.QuanZhong.Text, out qz))
            {
                this.QuanZhong.Text = "0";
            }
            decimal dj;
            if (!decimal.TryParse(this.DanJia.Text, out dj))
            {
                this.Notice = "“单价”录入有误";
                return;
            }
            if (this.BillID.Text == "")
            {
                this.SqlDataSource2.Insert();
            }
            else
            {
                if (this.SqlDataSource2.Update() > 0)
                {
                    this.Notice = "保存成功";
                }
            }

        }

        protected void SqlDataSource2_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[0].Value))
            {
                this.BillID.Text = ((int)e.Command.Parameters[0].Value).ToString();
                this.Notice = "保存成功";
            }
            else
            {
                this.Notice = "保存失败";
            }
        }

        protected void SqlDataSource2_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[1].Value))
            {
                this.BillID.Text = ((int)e.Command.Parameters[0].Value).ToString();
                this.PinMing.Text = (string)e.Command.Parameters[1].Value;
                if (!Convert.IsDBNull(e.Command.Parameters[2].Value))
                {
                    this.GuiGe.Text = (string)e.Command.Parameters[2].Value;
                }
                if (!Convert.IsDBNull(e.Command.Parameters[3].Value))
                {
                    this.DanWei.Text = (string)e.Command.Parameters[3].Value;
                }
                if (!Convert.IsDBNull(e.Command.Parameters[4].Value))
                {
                    this.DanJia.Text = ((double)e.Command.Parameters[4].Value).ToString();
                }
                if (!Convert.IsDBNull(e.Command.Parameters[5].Value))
                {
                    this.BianMa.Text = (string)e.Command.Parameters[5].Value;
                }
                if (!Convert.IsDBNull(e.Command.Parameters[6].Value))
                {
                    this.JianMa.Text = (string)e.Command.Parameters[6].Value;
                }
                if (!Convert.IsDBNull(e.Command.Parameters[7].Value))
                {
                    this.QuanZhong.Text = ((int)e.Command.Parameters[7].Value).ToString();
                }
            }
        }
    }
}
